<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let loginList = [
        { userName: 'admin', password: 'admin321' },
        { userName: 'zhangsan', password: 'zs321' },
        { userName: 'lisi', password: 'ls321' },
        { userName: 'yyqx', password: 'yyqx321' },
      ]
      let userInfo = {
        token1: {
          city: '杭州',
          money: '1000w',
          sex: '女',
          userName: 'admin',
          password: 'admin321',
        },
        token2: {
          city: '北京',
          money: '1100w',
          sex: '女',
          userName: 'zhangsan',
          password: 'zs321',
        },
        token3: {
          city: '北京',
          money: '800w',
          sex: '男',
          userName: 'lisi',
          password: 'ls321',
        },
        token4: {
          city: '天津',
          money: '600w',
          sex: '男',
          userName: 'yyqx',
          password: 'yyqx321',
        },
      }
      function fn(arr, user) {
        let uname = prompt('请输入你的账号')
        let pwd = prompt('请输入你的密码')
        let num = Math.floor(Math.random() * 4) + 1
        let count = 'token' + num
        // console.log(info)

        for (let i = 0; i < arr.length; i++) {
          if (uname === arr[i].userName && pwd === arr[i].password) {
            for (let k in user) {
              if (count === k && uname === user[k].userName) {
                count = user[k]
              }
              // for (let key in count) {
              //   console.log(count[key])
              // }
            }
            return alert('登录成功,具体用户信息：' + user[count])
          } else {
            let action = +prompt(`
            账户不存在
            1.注册账号
             2.直接退出`)
            if (action === 1) {
              let kong = {}
              let newName = prompt('请输入用户名')
              let newPwd = prompt('请输入新密码')
              alert('你已经成功注册，直接去登录')
              kong.userName = newName
              kong.password = newPwd
              arr.push(kong)
              console.log(arr)
              let uname = prompt('请输入你的账号')
              let pwd = prompt('请输入你的密码')
              for (let i = 0; i < arr.length; i++) {
                if (uname === arr[i].userName && pwd === arr[i].password) {
                  return alert('登录成功')
                } else {
                  return alert('再次输错，已无机会，直接退出了')
                }
              }
            }
            if (action === 2) {
              break
            }
          }
        }
      }
      fn(loginList, userInfo)
    </script>
  </body>
</html>
